home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 November: Tool Chest / Dev.CD Nov 00 TC Disk 2.toast / pc / sample code / quicktime / basics / qtcreatemovie / createmovie.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-09-28  |  8.0 KB  |  310 lines

  1. /*
  2.     File:        CreateMovie.c
  3.     
  4.     Contains:    QuickTime CreateMovie sample code
  5.     
  6.     Written by:    Scott Kuechle
  7.                 (based heavily on QuickTime sample code in Inside Macintosh: QuickTime)
  8.  
  9.     Copyright:    © 1998 by Apple Computer, Inc. All rights reserved
  10.     
  11.     Change History (most recent first)
  12.     
  13.           <3>         09/30/98    rtm        tweaked calls to CreateMovieFIle and AddMovieResource to create single-fork movies
  14.         <2>        09/28/98    rtm        changes for Metrowerks compiler
  15.         <1>        06/26/98    srk        first file
  16.  
  17.  
  18. */
  19.  
  20.  
  21. /************************************************************
  22. *                                                           *
  23. *    INCLUDE FILES                                          *
  24. *                                                           *
  25. *************************************************************/
  26.  
  27.  
  28. #if !defined(_MSC_VER) && _WIN32
  29.     #include <Win32Headers.mch>
  30.     #define TARGET_OS_WIN32            1
  31. #else
  32.     #include <ConditionalMacros.h>
  33. #endif
  34.  
  35. #if TARGET_OS_WIN32
  36.     #include <QTML.h>
  37.     #define    STRICT
  38.     #include <windows.h>
  39. #endif
  40.  
  41.  
  42. #include "MacTypes.h"
  43.  
  44. #include "MacMemory.h"
  45. #include "Errors.h"
  46. #include "Fonts.h"
  47. #include "QuickDraw.h"
  48. #include "Resources.h"
  49. #include "Gestalt.h"
  50. #include "FixMath.h"
  51. #include "Sound.h"
  52. #include "string.h"
  53. #include "Movies.h"
  54. #include "ImageCompression.h"
  55. #include "Script.h"
  56. #include "TextUtils.h"
  57. #include "Processes.h"
  58.  
  59.  
  60. #include "CreateMovie.h"
  61. #include "QTSound.h"
  62. #include "QTVideo.h"
  63.  
  64.  
  65. /************************************************************
  66. *                                                           *
  67. *    FUNCTION PROTOTYPES                                    *
  68. *                                                           *
  69. *************************************************************/
  70.  
  71. #if TARGET_OS_MAC
  72.     static void Utils_Macintosh_DisplayMsg(char *msg);
  73.     static void InitMacToolbox (void);
  74. #else if TARGET_OS_WIN32
  75.     static void Utils_Win32_DisplayMsg(char *msg);
  76. #endif
  77.  
  78. static void CreateAMovie (void);
  79. static void QuickTimeInit (void);
  80.  
  81.  
  82. /************************************************************
  83. *                                                           *
  84. *    CONSTANTS                                              *
  85. *                                                           *
  86. *************************************************************/
  87.  
  88. #define kMsgDialogRsrcID    129
  89. #define kMsgItemID            3    
  90. #define kPrompt                "Enter movie file name:"
  91.  
  92. #define kMovieFileName        "MovieFile.mov"
  93. #define kResName            "Movie Resource"
  94.  
  95. /*
  96. Sample Player's creator type since it is the movie player 
  97. of choice. You can use your own creator type, of course.
  98. */
  99. #define kMyCreatorType        FOUR_CHAR_CODE('TVOD')
  100.  
  101.  
  102. /************************************************************
  103. *                                                           *
  104. *    FUNCTIONS                                              *
  105. *                                                           *
  106. *************************************************************/
  107.  
  108. #if TARGET_OS_MAC
  109.  
  110. void main( void )
  111. {
  112.     InitMacToolbox ();
  113.     QuickTimeInit();
  114.     CreateAMovie ();
  115. }
  116.  
  117. #else if TARGET_OS_WIN32
  118.  
  119. int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
  120.                     LPSTR lpszCmdLine, int nCmdShow)
  121.  
  122. {
  123.     QuickTimeInit();
  124.     CreateAMovie ();
  125.  
  126.     return 0;
  127. }
  128.  
  129. #endif
  130.  
  131.  
  132.  
  133. /************************************************************
  134. *                                                           *
  135. *    CheckError()                                           *
  136. *                                                           *
  137. *    Displays error message if an error occurred            *
  138. *                                                           *
  139. *************************************************************/
  140.  
  141. void CheckError(OSErr error, char *msg)
  142. {
  143.     if (error == noErr)
  144.     {
  145.         return;
  146.     }
  147.     if (strlen(msg) > 0)
  148.     {
  149.         #if TARGET_OS_MAC
  150.             Utils_Macintosh_DisplayMsg(msg);
  151.         #else if TARGET_OS_WIN32
  152.             Utils_Win32_DisplayMsg(msg);
  153.         #endif
  154.         
  155.  
  156.         ExitToShell();
  157.  
  158.     }
  159. }
  160.  
  161. /************************************************************
  162. *                                                           *
  163. *    InitMacToolbox()                                       *
  164. *                                                           *
  165. *    Initializes the various Mac Toolbox Managers           *
  166. *                                                           *
  167. *************************************************************/
  168.  
  169.  
  170. #if TARGET_OS_MAC
  171.  
  172. static void InitMacToolbox (void)
  173. {
  174.     InitGraf (&qd.thePort);
  175.     InitFonts ();
  176.     InitWindows ();
  177.     InitMenus ();
  178.     TEInit ();
  179.     InitDialogs (nil);
  180. }
  181.  
  182. #endif
  183. /************************************************************
  184. *                                                           *
  185. *    QuickTimeInit()                                        *
  186. *                                                           *
  187. *    Initializes Quicktime                                  *
  188. *                                                           *
  189. *************************************************************/
  190.  
  191. static void QuickTimeInit (void)
  192. {
  193.     OSErr err;
  194.  
  195.         #if TARGET_OS_WIN32
  196.             err = InitializeQTML(0L);
  197.             CheckError (err, "InitializeQTML error" );
  198.         #endif
  199.  
  200.         err = EnterMovies ();
  201.  
  202.         CheckError (err, "EnterMovies error" );
  203. }
  204.  
  205. /************************************************************
  206. *                                                           *
  207. *    CreateAMovie()                                         *
  208. *                                                           *
  209. *    Creates a QuickTime movie with both a sound & video    *
  210. *    track                                                  *
  211. *                                                           *
  212. *************************************************************/
  213.  
  214. static void CreateAMovie (void)
  215. {
  216.     Point where = {100,100};
  217.     SFReply theSFReply;
  218.  
  219.     Movie theMovie = nil;
  220.     FSSpec mySpec;
  221.     short resRefNum = 0;
  222.     short resId = movieInDataForkResID;
  223.     OSErr err = noErr;
  224.  
  225.         SFPutFile (where, c2pstr(kPrompt), 
  226.                     c2pstr(kMovieFileName), nil, &theSFReply);
  227.         if (!theSFReply.good) return; 
  228.  
  229.         FSMakeFSSpec(theSFReply.vRefNum, 0, theSFReply.fName, &mySpec);
  230.  
  231.  
  232.         err = CreateMovieFile (&mySpec, 
  233.                             kMyCreatorType,
  234.                             smCurrentScript, 
  235.                             createMovieFileDeleteCurFile | createMovieFileDontCreateResFile,
  236.                             &resRefNum, 
  237.                             &theMovie );
  238.         CheckError(err, "CreateMovieFile error");
  239.  
  240.         QTVideo_CreateMyVideoTrack (theMovie);
  241.         QTSound_CreateMySoundTrack (theMovie);
  242.  
  243.  
  244.         err = AddMovieResource (theMovie, resRefNum, &resId,
  245.                                  theSFReply.fName);
  246.         CheckError(err, "AddMovieResource error");
  247.  
  248.         if (resRefNum)
  249.  
  250.         {
  251.  
  252.             CloseMovieFile (resRefNum);
  253.  
  254.         }
  255.         DisposeMovie (theMovie);
  256.  
  257.  
  258. /************************************************************
  259. *                                                           *
  260. *    FUNCTION:  Utils_Macintosh_DisplayMsg                  *
  261. *                                                           *
  262. *    PURPOSE:   Displays Macintosh error messages           *
  263. *                                                           *
  264. *************************************************************/
  265.  
  266. #if TARGET_OS_MAC
  267. static void Utils_Macintosh_DisplayMsg(char *msg)
  268. {
  269.     DialogPtr theDlog;
  270.     Handle item = NULL;
  271.     Rect box;
  272.  
  273.         theDlog = GetNewDialog(kMsgDialogRsrcID, NULL, (WindowPtr)-1);
  274.         if (theDlog != NULL)
  275.         {
  276.             short itemType;
  277.             
  278.                 GetDialogItem(theDlog, kMsgItemID, &itemType, &item, &box);
  279.                 if (item != NULL)
  280.                 {
  281.                     short itemHit;
  282.                     
  283.                         SetDialogItemText(item, c2pstr(msg));
  284.                         ModalDialog(NULL, &itemHit);
  285.                         DisposeDialog(theDlog);
  286.                         p2cstr((StringPtr)msg);    /* restore C-string */
  287.                 }
  288.         }
  289.  
  290. }
  291. #endif
  292.  
  293. /************************************************************
  294. *                                                           *
  295. *    FUNCTION:  Utils_Win32_DisplayMsg                      *
  296. *                                                           *
  297. *    PURPOSE:   Displays error messages for Win95/NT sample *
  298. *               code                                        *
  299. *                                                           *
  300. *************************************************************/
  301.  
  302. #if TARGET_OS_WIN32
  303. static void Utils_Win32_DisplayMsg(char *msg)
  304. {
  305.  
  306.     MessageBox(NULL, msg, "", MB_OK);
  307. }
  308. #endif
  309.